import { getLyric } from "api/song";
import { ERR_OK } from "api/config";
import { Base64 } from "js-base64";
export default class Song {
  constructor({ id, mid, singer, name, album, duration, image, url }) {
    this.id = id;
    this.mid = mid;
    this.singer = singer;
    this.name = name;
    this.album = album;
    this.duration = duration;
    this.image = image;
    this.url = url;
  }

  getLyric() {
    getLyric(this.mid).then(res => {
      if (res.retcode === ERR_OK) {
        this.lyric = Base64.decode(res.lyric);
        console.log(this.lyric);
        //   resolve(this.lyric);
      }
    });
  }
}

// 这个函数拿到原始数据后，详细的罗列整理属性
export function createSong(musicData, songVkey) {
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${
      musicData.albummid
    }.jpg?max_age=2592000`,
    // url: `http://ws.stream.qqmusic.qq.com/C100${musicData.songmid}.m4a?fromtag=0&guid=126548448`
    url: `http://aqqmusic.tc.qq.com/amobile.music.tc.qq.com/C400${
      musicData.songmid
    }.m4a?vkey=${songVkey}&guid=6319873028&uin=0&fromtag=38`
  });
}
// 在这里处理musicData中的singer
export function filterSinger(singer) {
  let ret = [];
  // 如果传入的singer是空，那么我们也return空
  if (!singer) {
    return "";
  }
  // 否则push name到ret里
  singer.forEach(element => {
    ret.push(element.name);
  });
  return ret.join("/");
}
